package portal.conf;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.GPUEntity;
import portal.conf.entity.QueueEntity;
@Component
@Scope("singleton")
public class QueueConf extends AbstractConf {
	//private  long lastModified=-1;
	//private  Vector<Object> confList =new Vector<>();
	//private  String fileName="//app//unischeduler//conf//queues.conf";
	private static String startTitle="Begin Queue";
	private static String endTitle="End Queue";
	private static String[]  ele={
			"QUEUE_NAME",//// idle
			"PRIORITY",//// 20
			"NICE",//// 20
			"UJOB_LIMIT",
			"QJOB_LIMIT",
			"RUN_WINDOW",
			"r15s",//// 0.3/1.5
			"r1m",//// 0.3/1.5
			"pg",//// 4.0/15
			"it",//// 10/1
			"CPULIMIT",
			"STOP_COND",
			"MEMLIMIT",//// 5000 " jobs bigger than this (5M) will be niced
			"FILELIMIT",//// 20000
			"DATALIMIT",//// 20000 " jobs data segment limit
			"STACKLIMIT",//// 2048
			"CORELIMIT",//// 20000
			"PROCLIMIT",//// 5
			"PREEMPTION",
			"RUNLIMIT",
			"USERS",////
			"HOSTS",////
			"ADMINISTRATORS",////
			"PRE_EXEC",//// 
			"POST_EXEC",////
			"REQUEUE_EXIT_VALUES",////
			"BIND_CPU",//// BALANCE
			"RES_REQ",
			"DESCRIPTION"//,//// Running only if the machine is idle and very lightly loaded.
	};
	public QueueConf(){
		if(IsDebug)
		setFileName("/apps/NSE/conf/queues.conf");
	}
	@Value("${conf.queuefile}")  
	public void setFileName(String fname) {  
		super.setFileName(fname);//fileName = fname;  
	}  
	protected String[] getElements() {
		return ele;
	}
	public static String[] getElementsP(){
		return ele;
	}
	@Value("${conf.queuefile.start}")  
	public void setStartTitle(String start) {  
		startTitle = start;  
	}  
	@Value("${conf.queuefile.end}")  
	public void setEndTitle(String end) {  
		endTitle = end;  
	}  
	public static String getStartTitle(){
		return startTitle;
	}
	public static String getEndTitle(){
		return endTitle;
	}
	
	@Override
	protected boolean isConfStart(String line) {
		// TODO Auto-generated method stub
		if(line.contains("Begin Queue"))
			return true;
		return false;
	}

	@Override
	protected boolean isConfEnd(String line) {
		// TODO Auto-generated method stub
		if(line.contains("End Queue"))
			return true;
		return false;
	}
	@Override
	protected  BaseConf analysiConf(String tempString,int line,Class<?> t,BaseConf qe){
		//System.out.println(t.getName());
		//BaseConf qe=null;
		try {
			int i=0;
			if(qe==null)
				qe=(BaseConf)(t.newInstance());
			//String[] ele= getElements();
			
			if(isConfStart(tempString)){///配置块的头进行解析，并生成一个新对象
				//if(qe==null)
				qe=(BaseConf)(t.newInstance());
				qe.setLineStart(line);
				return qe;
			}
			if(isConfEnd(tempString)&& qe!=null){	///配置块的尾进行解析，并生成一个新对象			
				qe.setLineEnd(line);
				addToConfList(qe);
				return qe;
			}			
			for(i=0;i<ele.length;i++){
				if(tempString.contains(ele[i]))
					break;
			}
			//System.out.println(tempString);		
			if(i<ele.length){
				//return qe;
				Method method =Class.forName(t.getName()).getMethod("set"+MyStringUtils.upperFirstCase(ele[i]),String.class);	
				method.invoke(qe,MyStringUtils.trimAll(tempString.substring(tempString.indexOf("=")+1)));
			}
			//return qe;
		} catch (NoSuchMethodException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return qe;
	}

	public boolean insertConf(BaseConf qe){
		if(!isEntityExist(qe)){
			insertConf(qe.toString());
			return true;
		}
		return false;
	}
	public void insertConf(String data){
		//if(isEntityExist
		insertConf(fileName,data.split("\n"));
		selectAllConf(new File(fileName),QueueEntity.class);
		//this.selectAllConf(fileName, QueueEntity.class);
	}
	public void insertConf(String[] data){
		//selectAll
		insertConf(fileName,data);
		selectAllConf(new File(fileName),QueueEntity.class);//this.selectAllConf(fileName, QueueEntity.class);
	}
	public boolean removeConf(String name){
		//selectAllConf();
		removeConf(findConfByName(name));
		selectAllConf(new File(fileName),QueueEntity.class);//this.selectAllConf(fileName, QueueEntity.class);
		return true;
	}
	public List<BaseConf> selectAllConf(){
		return selectAllConf(fileName, QueueEntity.class);
	}

	//@Test
	public static void main(String[] args){
		QueueConf queueConf=new QueueConf();
		List<BaseConf> list=queueConf.selectAllConf();
		QueueEntity qe=new QueueEntity();
		//qe.setQUEUE_NAME("xuwenyuantest");
		//qe.setCORELIMIT(1000);
		//System.out.println(qe);
		//queueConf.insertConf(qe);
		queueConf.removeConf("xuwenyuantest");
		//queueConf.re
		//list=queueConf.selectAllConf();
		//list=queueConf.selectAllConf();
		System.out.println(list.size());
		for(int index=0;index<list.size();index++){
			System.out.println(list.get(index));
		}
	}

}
